<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="title" :visible.sync="dialogVisible" width="20%" :before-close="handleClose">
        <el-form ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="品种图片">
                <UpLoadImage v-if="dialogVisible" @getFileRawList="getFileRawList" :limit="1" :size="1"></UpLoadImage>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 认</el-button>
        </span>
    </el-dialog>
</template>
<script>
import UpLoadImage from "@/components/UpLoadImage";
import $baseUrl from '@/utils/baseURL'
import axios from "axios";
export default {
    components: { UpLoadImage },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            addEditApi: '',
            filId: '',
            formData: {},
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        getFileRawList(data) {
            let fileRawList = data.fileRawList;
            let formData = new FormData();
            formData.append("organizationNo", sessionStorage.organizationNo);
            formData.append("plantVarietyNo", this.data.plantVarietyNo);
            if(this.data.pictureNo){
                formData.append("plantVarietyPictureNo", this.data.pictureNo);
            }
            formData.append("file", fileRawList[0]);
            this.formData = formData
        },
        handleClick(tab, event) {
            console.log(tab, event);
        },
        initData() {
            if (this.addEditFlag == 'add') {
                this.addEditApi = $baseUrl.agricultureServiceUrl + 'v3/templateTenant/plantVarietyPicture/add'
            } else if (this.addEditFlag == 'edit') {
                this.addEditApi = $baseUrl.agricultureServiceUrl + 'v3/templateTenant/plantVarietyPicture/edit'
            }
        },
        //提交表单
        submitForm() {
            axios({
                url: this.addEditApi,
                method: "post",
                headers: {
                    databaseName: sessionStorage.databaseName,
                    accessToken: sessionStorage.token,
                    enctype: "multipart/form-data",
                    organizationList: sessionStorage.organizationList,
                    organizationNo: sessionStorage.organizationNo,
                    virtualSystemNo: sessionStorage.virtualSystemNo
                },
                data: this.formData
            }).then(res => {
                if (res.data.code === 1) {
                    this.cancel();
                    this.$message.success("操作成功！");
                } else {
                    this.$message.warning("操作失败！");
                }
            });
        },
        //重置
        resetForm() {
            this.addEditApi = '';
            this.filId = '';
            this.formData = {};
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>